package calbs.socket.service;/**
 * Created by zhangqin on 2017/8/7.
 */

import calbs.socket.dao.ServerDao;
import calbs.socket.enu.FrameReqEnum;
import calbs.socket.enu.FrameRespEnum;
import calbs.socket.exception.AnalySocketException;
import calbs.socket.exception.AnalySocketExceptionEmum;
import calbs.socket.pojo.req.*;
import calbs.socket.pojo.resp.*;
import calbs.socket.server.cache.UserCache;
import calbs.socket.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import java.util.Date;

import static java.lang.Integer.parseInt;

/**
 * 数据库服务
 *
 * @author
 * @create 2017-08-07 15:26
 **/
@Service
public class ServerService {
    private static final Logger logger = LoggerFactory.getLogger(ServerService.class);
    private ServerDao serverDao = SpringUtil.getBean(ServerDao.class);
    @Resource
    private JmsTemplate jmsTemplate;
    @Resource
    private Queue zdjsLocationQueue;
    public RespData getRespData(ReqData reqData){
        switch (FrameReqEnum.getFrameEnum(reqData.getBody().getFrameNumber())){
            case CMD_NOP:
                return  getNullFrame();
            case DS_GETLINE:
                return getRespAbLineListMsg(reqData);
            case DS_POS:
                return getRespUploadLocationMsg(reqData);
            case DS_ALARMCODE:
                return getRespWaringMsg(reqData);
            case DS_UPDATE:
                return getRespSoftUpdateVersionMsg(reqData);
            case DS_ABLINE_UPLOAD:
                return getRespUploadAbLineMsg(reqData);
            case DS_LOGIN:
                if(isloginAccept(reqData)==true)
                    return getRespLoginAcceptMsg(reqData);
                else
                    return getRespLoginRefuseMsg(reqData);
            default:
              throw new AnalySocketException(AnalySocketExceptionEmum.SYS_ERROR);
        }
    }

    /**
     * 获取AB线
     * @param reqGetAbLineMsgReqData
     * @return
     */
    public RespData<RespAbLineListMsg> getRespAbLineListMsg(ReqData<ReqGetAbLineMsg> reqGetAbLineMsgReqData){
        ReqGetAbLineMsg body = reqGetAbLineMsgReqData.getBody();
        RespAbLineListMsg abLineListMsg = serverDao.getRespAbLineList(Double.parseDouble(body.getLatitude()),Double.parseDouble(body.getLongitude()));
        abLineListMsg.setFrameNumber(FrameRespEnum.SD_GETLINE.getFrameNumber());
        return RespData.build().buildBody(abLineListMsg);
    }

    /**
     * 上传位置信息
     * @param reqUploadLocationMsgReqData
     * @return
     */
    public RespData<RespFinishMsg> getRespUploadLocationMsg(ReqData<ReqUploadLocationMsg> reqUploadLocationMsgReqData){
        ReqUploadLocationMsg body =reqUploadLocationMsgReqData.getBody();
        jmsTemplate.send(zdjsLocationQueue, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(JSON.toJSONString(body));
            }
        });
        RespFinishMsg finishMsg = new RespFinishMsg();
        finishMsg.setReceiveContentLength(body.getFrameContentLength());
        finishMsg.setReceiveFrameNumber(body.getFrameNumber());
        finishMsg.setFrameNumber(FrameRespEnum.SD_FINISH.getFrameNumber());
        return RespData.build().buildBody(finishMsg);
    }

    /**
     * 警告
     * @param reqUploadLocationMsgReqData
     * @return
     */
    public RespData<RespFinishMsg> getRespWaringMsg(ReqData<ReqUploadWarningMsg> reqUploadLocationMsgReqData){
        ReqUploadWarningMsg body =reqUploadLocationMsgReqData.getBody();
        serverDao.insertWarning(body);
        RespFinishMsg finishMsg = new RespFinishMsg();
        finishMsg.setReceiveContentLength(body.getFrameContentLength());
        finishMsg.setReceiveFrameNumber(body.getFrameNumber());
        finishMsg.setFrameNumber(FrameRespEnum.SD_FINISH.getFrameNumber());
        return RespData.build().buildBody(finishMsg);
    }

    /**
     * 获取最新版本
     * @param reqGetSoftUpdateVersionMsgReqData
     * @return
     */
    public RespData<RespSoftUpdateVersionMsg> getRespSoftUpdateVersionMsg(ReqData<ReqGetSoftUpdateVersionMsg> reqGetSoftUpdateVersionMsgReqData){
        RespSoftUpdateVersionMsg softUpdateVersionMsg =serverDao.getNewVersion();
        softUpdateVersionMsg.setFrameNumber(FrameRespEnum.SD_UPDATE.getFrameNumber());
        return RespData.build().buildBody(softUpdateVersionMsg);
    }

    /**
     * 上传AB线
     * @param reqUploadAbLineMsgReqData
     * @return
     */
    public RespData<RespUploadAbLineMsg> getRespUploadAbLineMsg(ReqData<ReqUploadAbLineMsg> reqUploadAbLineMsgReqData){
        ReqUploadAbLineMsg body = reqUploadAbLineMsgReqData.getBody();
        RespUploadAbLineMsg respUploadAbLineMsg = serverDao.uploadAbLineMsg(body);
        respUploadAbLineMsg.setFrameNumber(FrameRespEnum.SD_UPLOADLINE.getFrameNumber());
        return RespData.build().buildBody(respUploadAbLineMsg);
    }

    /**
     * 登陆接受
     * @param reqLoginMsgReqData
     * @return
     */
    public RespData<RespLoginAcceptMsg> getRespLoginAcceptMsg(ReqData<ReqLoginMsg> reqLoginMsgReqData){

        ReqLoginMsg body = reqLoginMsgReqData.getBody();
        logger.info("login success:"+body.getDevice());
        RespLoginAcceptMsg respLoginAcceptMsg = new RespLoginAcceptMsg();
        respLoginAcceptMsg.setDevice(parseInt(body.getDevice()));
        respLoginAcceptMsg.setUtcTime(Integer.parseInt(new Date().getTime()/1000+""));
        respLoginAcceptMsg.setUserName(body.getUserName());
        respLoginAcceptMsg.setUserPassword(body.getUserPassword());
        respLoginAcceptMsg.setFrameNumber(FrameRespEnum.SD_ACCEPT.getFrameNumber());
        UserCache.add(body.getDevice(),reqLoginMsgReqData.getChannel());
        return RespData.build().buildBody(respLoginAcceptMsg);
    }

    /**
     * 登陆拒绝
     * @param reqLoginMsgReqData
     * @return
     */
    public RespData<RespLoginRefuseMsg> getRespLoginRefuseMsg(ReqData<ReqLoginMsg> reqLoginMsgReqData){

        RespLoginRefuseMsg respLoginRefuseMsg = new RespLoginRefuseMsg();
        logger.info("login error");
        respLoginRefuseMsg.setFrameNumber(FrameRespEnum.SD_REFUSE.getFrameNumber());
        respLoginRefuseMsg.setErrorId(01);
        return RespData.build().buildBody(respLoginRefuseMsg);
    }
    /**
     * 空针
     * @return
     */
    public RespData<RespLoginRefuseMsg> getNullFrame(){
        RespPingMsg respLoginRefuseMsg = new RespPingMsg();
        respLoginRefuseMsg.setFrameNumber(FrameRespEnum.CMD_NOP.getFrameNumber());
        return RespData.build().buildBody(respLoginRefuseMsg);
    }

    /**
     * 判断登陆是否成功
     * @param reqLoginMsgReqData
     * @return
     */
    public boolean isloginAccept(ReqData<ReqLoginMsg> reqLoginMsgReqData){
        ReqLoginMsg body = reqLoginMsgReqData.getBody();
        return serverDao.hasDevice(body.getDevice());
    }
}
